﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using SPW.SystemHeader.Views;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
using SyntacticSugar;
using SPW.Infrastructure.Interfaces;

namespace SPW.SystemHeader.ViewModels
{
    public class DefaultHeaderViewModel : BindableBase
    {
        private bool _menuToggleChecked = false;

        public bool MenuToggleChecked
        {
            get => _menuToggleChecked;
            set
            {
                SetProperty(ref _menuToggleChecked, value);
                if (value)
                {
                    //发布订阅消息
                    this._eventAggregator.GetEvent<Infrastructure.Events.DisplayMenuEvent>().Publish(new Infrastructure.Events.MenuToggleEvent());
                }
            }
        }

        private string _headerTitle;

        public string HeaderTitle
        {
            get => _headerTitle;
            set => SetProperty(ref _headerTitle, value);
        }

        public string NickName { get; private set; }

        private readonly IEventAggregator _eventAggregator;
        private readonly IDataServiceClient _dataServiceClient;

        public DefaultHeaderViewModel(IEventAggregator eventAggregator,
            IDataServiceClient dataServiceClient)
        {
            this._eventAggregator = eventAggregator;
            this._dataServiceClient = dataServiceClient;
            NickName = dataServiceClient.NickName;
            HeaderTitle = "时空大数据平台";
            SubscribeEvents();
        }

        /// <summary>
        /// 订阅事件
        /// </summary>
        private void SubscribeEvents()
        {
            //订阅事件
            this._eventAggregator.GetEvent<Infrastructure.Events.HideMenuEvent>().Subscribe((e) => { MenuToggleChecked = false; });
        }

        public ICommand ExitApplicationCommand
        {
            get => new DelegateCommand(() =>
            {
                //发送消息
                this._eventAggregator.GetEvent<Infrastructure.Events.DialogEvent>()
                .Publish(new Infrastructure.Events.DialogEventArgs
                {
                    DialogModel = Infrastructure.Events.DialogEventArgs.DialogModelModes.Confirm,
                    Message = "您确认立即退出本应用？",
                    Callback = (a) => 
                    {
                        if(a.IsTrue()) Process.GetCurrentProcess().Kill();
                    }
                });
            });
        }
    }
}
